home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / esc.jar / com / extensibility / xa / HtmlTableDoc.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-06-30  |  7.8 KB  |  352 lines

  1. package com.extensibility.xa;
  2.  
  3. import com.extensibility.app.BaseApplication;
  4. import com.extensibility.app.DialogFactory;
  5. import com.extensibility.app.UI;
  6. import com.extensibility.rock.GifEncoder;
  7. import com.extensibility.rock.ImageEncoder;
  8. import com.extensibility.xml.URI;
  9. import com.extensibility.xml.XMLStringUtilities;
  10. import java.awt.Component;
  11. import java.awt.Image;
  12. import java.io.BufferedReader;
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.FileReader;
  16. import java.io.FileWriter;
  17. import java.io.IOException;
  18. import java.io.StringReader;
  19. import java.io.Writer;
  20. import java.net.MalformedURLException;
  21. import java.net.URL;
  22. import java.util.Hashtable;
  23. import javax.swing.JTable;
  24.  
  25. public final class HtmlTableDoc implements HtmlTableDocWriter {
  26.    protected static final String TAG_HTML_DECL = "html";
  27.    protected static final String TAG_BODY_DECL = "body";
  28.    protected static final String TAG_TABLE_DECL = "table";
  29.    protected static final String TAG_TR_DECL = "tr";
  30.    protected static final String TAG_TD_DECL = "td";
  31.    protected static final String TAG_TH_DECL = "th";
  32.    protected static final String TAG_CAPTION_DECL = "caption";
  33.    protected static final String TAG_IMG_DECL = "img";
  34.    protected static final String TABLE_TITLE = "title";
  35.    protected static final String TAG_COMMENT_START = "<!--";
  36.    protected static final String TAG_COMMENT_END = "-->";
  37.    protected static final String HTML_ITEM_START = "XAitem";
  38.    protected static final String HTML_TABLE_START = "XAtable";
  39.    protected static final String BEFORE_TABLE = "beforeTable";
  40.    protected static final String AFTER_TABLE = "afterTable";
  41.    protected static final String BEFORE_TR = "beforeTr";
  42.    protected static final String AFTER_TR = "afterTr";
  43.    protected static final String BEFORE_TH = "beforeTh";
  44.    protected static final String AFTER_TH = "afterTh";
  45.    protected static final String BEFORE_TD = "beforeTd";
  46.    protected static final String AFTER_TD = "afterTd";
  47.    private static final String IMAGE_BASE_NAME = "XAImageFile";
  48.    protected static final int COL_START = 2;
  49.    protected static final int ROW_START = 0;
  50.    private File spoolFile;
  51.    private File templateFile;
  52.    private int imageCount = 0;
  53.    private Writer writer;
  54.    private HtmlWriter htmlWriter;
  55.    private BufferedReader inReader;
  56.    private String tableTag;
  57.    private String trTag;
  58.    private String thTag;
  59.    private String tdTag;
  60.    private String captionTag;
  61.    private String lineStr;
  62.    private boolean firstTable;
  63.    boolean hasTable = false;
  64.  
  65.    public HtmlTableDoc(File var1, File var2) {
  66.       this.spoolFile = var2;
  67.       this.templateFile = var1;
  68.    }
  69.  
  70.    public File getSpoolFile() {
  71.       return this.spoolFile;
  72.    }
  73.  
  74.    public URL getSpoolURL() {
  75.       try {
  76.          URL var1 = new URL((new URI(this.spoolFile)).getFullName());
  77.          return var1;
  78.       } catch (MalformedURLException var3) {
  79.          Object var2 = null;
  80.          return (URL)var2;
  81.       }
  82.    }
  83.  
  84.    public void createTemplateReader() throws IOException {
  85.       this.inReader = new BufferedReader(new FileReader(this.templateFile));
  86.    }
  87.  
  88.    public void createSpoolWriter() throws IOException {
  89.       this.writer = new FileWriter(this.spoolFile);
  90.       this.htmlWriter = new HtmlWriter(this.writer);
  91.    }
  92.  
  93.    public void writeHtmlHeader() throws IOException {
  94.       while((this.lineStr = this.inReader.readLine()) != null && this.lineStr.indexOf("<body") == -1) {
  95.          this.htmlWriter.writeText(this.lineStr);
  96.          this.htmlWriter.writeLineBreak(false);
  97.       }
  98.  
  99.       if (this.lineStr != null) {
  100.          this.htmlWriter.writeText(this.lineStr);
  101.          this.htmlWriter.writeLineBreak(false);
  102.       }
  103.  
  104.    }
  105.  
  106.    public void scanTemplate(HtmlTableDocClient var1, String[] var2, int[] var3) {
  107.       try {
  108.          this.createTemplateReader();
  109.          this.createSpoolWriter();
  110.          this.writeHtmlHeader();
  111.  
  112.          while(true) {
  113.             int var4;
  114.             for(this.lineStr = this.inReader.readLine(); (var4 = this.lineStr.indexOf("XAitem")) == -1; this.lineStr = this.inReader.readLine()) {
  115.                this.htmlWriter.writeText(this.lineStr);
  116.                this.htmlWriter.writeLineBreak(false);
  117.             }
  118.  
  119.             int var5 = this.findMatch(this.lineStr, var2);
  120.             if (var5 >= 0) {
  121.                Hashtable var6 = XMLStringUtilities.getNameValuePairs(this.lineStr.substring(var4 + "XAitem".length(), this.lineStr.indexOf("-->", var4)));
  122.                var1.scanMatchItem(this, var6, var3[var5]);
  123.             }
  124.          }
  125.       } catch (Exception var7) {
  126.          this.flush();
  127.          this.close();
  128.       }
  129.    }
  130.  
  131.    public void writeImage(Image var1, Hashtable var2, int var3) {
  132.       BaseApplication.getApplication();
  133.       File var4 = BaseApplication.getHomePath();
  134.       StringBuffer var5 = new StringBuffer("XAImageFile");
  135.       var5.append(this.imageCount++);
  136.       var5.append(".gif");
  137.       File var6 = new File(var4, var5.toString());
  138.       ++this.imageCount;
  139.       StringBuffer var7 = new StringBuffer("<p>");
  140.       var7.append(var2.get("title"));
  141.       var7.append("</p>\r\n<p>");
  142.       var7.append("<img border=\"0\" src=\"");
  143.       var7.append(var5);
  144.       var7.append("\"></p>");
  145.       this.htmlWriter.writeRawText(var7.toString());
  146.       this.htmlWriter.writeLineBreak();
  147.       this.htmlWriter.writeRawText("<div>");
  148.       this.htmlWriter.writeLineBreak();
  149.  
  150.       try {
  151.          FileOutputStream var8 = new FileOutputStream(var6);
  152.          GifEncoder var9 = new GifEncoder(var1, var8);
  153.          ((ImageEncoder)var9).setColorMangler(new ImageEncoder.BackgroundColorMangler(var3));
  154.          ((ImageEncoder)var9).encode();
  155.          var8.close();
  156.       } catch (Exception var10) {
  157.       }
  158.  
  159.       this.hasTable = true;
  160.    }
  161.  
  162.    public void writeText(String var1) {
  163.       this.hasTable = true;
  164.       StringReader var2 = new StringReader(var1);
  165.       BufferedReader var3 = new BufferedReader(var2);
  166.  
  167.       try {
  168.          while(true) {
  169.             String var4 = var3.readLine();
  170.             this.htmlWriter.writeHtmlText(var4);
  171.             this.htmlWriter.writeRawText("<br>");
  172.             this.htmlWriter.writeLineBreak();
  173.          }
  174.       } catch (Exception var5) {
  175.       }
  176.    }
  177.  
  178.    public void writeTable(DeclTable var1, Hashtable var2) {
  179.       this.hasTable = true;
  180.  
  181.       try {
  182.          if (this.lineStr == null || var1 == null) {
  183.             return;
  184.          }
  185.  
  186.          String var3 = (String)var2.get("table");
  187.          String var4 = (String)var2.get("tr");
  188.          String var5 = (String)var2.get("td");
  189.          String var6 = (String)var2.get("th");
  190.          String var7 = (String)var2.get("caption");
  191.          String var8 = (String)var2.get("title");
  192.          String var9 = (String)var2.get("beforeTable");
  193.          String var10 = (String)var2.get("afterTable");
  194.          String var11 = (String)var2.get("beforeTh");
  195.          String var12 = (String)var2.get("afterTh");
  196.          String var13 = (String)var2.get("beforeTr");
  197.          String var14 = (String)var2.get("afterTr");
  198.          String var15 = (String)var2.get("beforeTd");
  199.          String var16 = (String)var2.get("afterTd");
  200.          if (var9 != null && var9.length() > 1) {
  201.             this.htmlWriter.writeText(var9);
  202.             this.htmlWriter.writeLineBreak(false);
  203.          }
  204.  
  205.          this.htmlWriter.writeElem("table", -1);
  206.          this.htmlWriter.writeAttr(var3, "");
  207.          if (var13 != null && var13.length() > 1) {
  208.             this.htmlWriter.writeText(var13);
  209.             this.htmlWriter.writeLineBreak(false);
  210.          }
  211.  
  212.          this.htmlWriter.writeElem("tr", -1);
  213.          this.htmlWriter.writeAttr(var4, "");
  214.  
  215.          for(int var17 = 2; var17 < ((JTable)var1).getColumnCount(); ++var17) {
  216.             String var18 = ((JTable)var1).getColumnName(var17);
  217.             var18 = UI.getString(var18);
  218.  
  219.             try {
  220.                this.htmlWriter.writeElem("th", -1);
  221.                this.htmlWriter.writeAttr(var6, "");
  222.                if (var11 != null && var11.length() > 1) {
  223.                   this.htmlWriter.writeText(var11);
  224.                   this.htmlWriter.writeLineBreak(false);
  225.                }
  226.  
  227.                this.htmlWriter.writeText(var18);
  228.                if (var12 != null && var12.length() > 1) {
  229.                   this.htmlWriter.writeText(var12);
  230.                   this.htmlWriter.writeLineBreak();
  231.                }
  232.  
  233.                this.htmlWriter.flush("th");
  234.             } catch (Exception var24) {
  235.                DialogFactory.showException((Component)null, 128, var24, "");
  236.             }
  237.          }
  238.  
  239.          this.htmlWriter.flush("tr");
  240.          if (var14 != null && var14.length() > 1) {
  241.             this.htmlWriter.writeText(var14);
  242.             this.htmlWriter.writeLineBreak(false);
  243.          }
  244.  
  245.          for(int var27 = 0; var27 < ((JTable)var1).getRowCount() - 1; ++var27) {
  246.             if (var13 != null && var13.length() > 1) {
  247.                this.htmlWriter.writeText(var13);
  248.                this.htmlWriter.writeLineBreak(false);
  249.             }
  250.  
  251.             this.htmlWriter.writeElem("tr", -1);
  252.             this.htmlWriter.writeAttr(var4, "");
  253.  
  254.             for(int var19 = 2; var19 < ((JTable)var1).getColumnCount(); ++var19) {
  255.                this.htmlWriter.writeElem("td", -1);
  256.                this.htmlWriter.writeAttr(var5, "");
  257.                if (var15 != null && var15.length() > 1) {
  258.                   this.htmlWriter.writeText(var15);
  259.                   this.htmlWriter.writeLineBreak(false);
  260.                }
  261.  
  262.                Object var20 = ((JTable)var1).getValueAt(var27, var19);
  263.                String var21 = "";
  264.                if (var20 != null) {
  265.                   var21 = var20.toString();
  266.                }
  267.  
  268.                if (var21.equals("false")) {
  269.                   var21 = "";
  270.                } else if (var21.equals("true")) {
  271.                   var21 = "X";
  272.                }
  273.  
  274.                if (var21.equals("")) {
  275.                   this.htmlWriter.writeText("<pre> </pre>");
  276.                } else {
  277.                   try {
  278.                      this.htmlWriter.writeText(var21);
  279.                   } catch (Exception var23) {
  280.                      DialogFactory.showException((Component)null, 128, var23, "");
  281.                   }
  282.                }
  283.  
  284.                if (var16 != null && var16.length() > 1) {
  285.                   this.htmlWriter.writeText(var16);
  286.                   this.htmlWriter.writeLineBreak(false);
  287.                }
  288.  
  289.                this.htmlWriter.flush("td");
  290.             }
  291.  
  292.             this.htmlWriter.flush("tr");
  293.             if (var14 != null && var14.length() > 1) {
  294.                this.htmlWriter.writeText(var14);
  295.                this.htmlWriter.writeLineBreak(false);
  296.             }
  297.          }
  298.  
  299.          this.htmlWriter.flush("table");
  300.          if (var10 != null && var10.length() > 1) {
  301.             this.htmlWriter.writeText(var10);
  302.             this.htmlWriter.writeLineBreak(false);
  303.          }
  304.       } catch (Exception var25) {
  305.          DialogFactory.showException((Component)null, 128, var25, "");
  306.          this.close();
  307.       }
  308.  
  309.    }
  310.  
  311.    public void flush() {
  312.       this.htmlWriter.flush();
  313.  
  314.       try {
  315.          while((this.lineStr = this.inReader.readLine()) != null) {
  316.             this.htmlWriter.writeText(this.lineStr);
  317.             this.htmlWriter.writeLineBreak(false);
  318.          }
  319.       } catch (Exception var2) {
  320.          DialogFactory.showException((Component)null, 128, var2, "");
  321.       }
  322.  
  323.    }
  324.  
  325.    public boolean hasTable() {
  326.       return this.hasTable;
  327.    }
  328.  
  329.    public void close() {
  330.       try {
  331.          this.inReader.close();
  332.          this.writer.close();
  333.       } catch (IOException var2) {
  334.          DialogFactory.showException((Component)null, 128, var2, "");
  335.       }
  336.  
  337.    }
  338.  
  339.    private int findMatch(String var1, String[] var2) {
  340.       int var3 = var2.length;
  341.       int var4 = -1;
  342.  
  343.       for(int var5 = 0; var5 < var3 && var4 < 0; ++var5) {
  344.          if (var1.indexOf(var2[var5]) != -1) {
  345.             var4 = var5;
  346.          }
  347.       }
  348.  
  349.       return var4;
  350.    }
  351. }
  352.